package com.yomob.kylin.support;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yomob.kylin.facility.Consts;
import com.yomob.kylin.support.joptsimple.util.LocalDateConverter;
import com.yomob.kylin.support.joptsimple.util.MetricConverter;
import com.yomob.kylin.support.joptsimple.util.StorageLevelNameConverter;

import joptsimple.OptionParser;
import joptsimple.OptionSet;

public class OptionSupport {
    private static final String MONGO_URI = "mongoURI";
    private static final String MONGO_URI_DFLT_VALUE = "mongodb://127.0.0.1:27017/";
    private static final String MONGO_DB = "mongoDB";
    private static final String MONGO_DB_DFLT_VALUE = "datatrack";
    private static final String HDFS_PATH = "hdfsPath";
    private static final String HDFS_PATH_DFLT_VALUE = "/events";
    private static final String START_DATE = "startDate";
    private static final String END_DATE = "endDate";
    private static final String METRICS = "metrics";
    private static final String STORAGE_LEVEL = "storageLevel";

    private static final Map<String, Object> options = new HashMap<>();

    public static Map<String, Object> getOptions() {
        return options;
    }

    public static void parse(String[] args) {
        OptionParser parser = new OptionParser(false);
        parser.accepts(MONGO_URI).withRequiredArg().defaultsTo(MONGO_URI_DFLT_VALUE);
        parser.accepts(MONGO_DB).withRequiredArg().defaultsTo(MONGO_DB_DFLT_VALUE);
        parser.accepts(HDFS_PATH).withRequiredArg().defaultsTo(HDFS_PATH_DFLT_VALUE);
        parser.accepts(START_DATE).withRequiredArg().withValuesConvertedBy(LocalDateConverter.ofPattern("yyyy-MM-dd")).required();
        parser.accepts(END_DATE).withRequiredArg().withValuesConvertedBy(LocalDateConverter.ofPattern("yyyy-MM-dd")).required();
        parser.accepts(METRICS).withRequiredArg().withValuesSeparatedBy(",").withValuesConvertedBy(new MetricConverter(Metric.class)).required();
        parser.accepts(STORAGE_LEVEL).withRequiredArg().withValuesConvertedBy(new StorageLevelNameConverter(StorageLevelName.class)).required();

        OptionSet opts = parser.parse(args);

        options.put(MONGO_URI, opts.valueOf(MONGO_URI));
        options.put(MONGO_DB, opts.valueOf(MONGO_DB));
        options.put(HDFS_PATH, opts.valueOf(HDFS_PATH));
        options.put(START_DATE, opts.valueOf(START_DATE));
        options.put(END_DATE, opts.valueOf(END_DATE));
        options.put(METRICS, opts.valuesOf(METRICS));
        options.put(STORAGE_LEVEL, opts.valueOf(STORAGE_LEVEL));
    }


    public static String getMongoUri() {
        return (String) options.getOrDefault(MONGO_URI, MONGO_URI_DFLT_VALUE);
    }

    public static String getMongoDb() {
        return (String) options.getOrDefault(MONGO_DB, MONGO_DB_DFLT_VALUE);
    }

    public static String getHdfsPath() {
        return (String) options.getOrDefault(HDFS_PATH, HDFS_PATH_DFLT_VALUE);
    }

    public static LocalDate getStartDate() {
        return (LocalDate) options.get(START_DATE);
    }

    public static LocalDate getEndDate() {
        return (LocalDate) options.get(END_DATE);
    }
    public static List<Metric> getMetrics() {
        return (List<Metric>) options.get(METRICS);
    }
    public static StorageLevelName getStorageLevel() {
        return (StorageLevelName) options.get(STORAGE_LEVEL);
    }

    private static String message(String errorKey, Object... values) {
        return Messages.message(Consts.Bundle.EXCEPTION_MESSAGES, OptionSupport.class, errorKey, values);
    }
}
